/*
   Multithread parse segments
*/

#include <stdio.h>
#include "comm.h"

/* parse every segment to pick domain */
void* parseSeg(t_segment *pSeg){
	PRINTF("Start thread No%d, start = %p, end = %p\n", pSeg->segno, pSeg->start, pSeg->end);

	BYTE *start = pSeg->start;
	BYTE *end   = pSeg->end;

	/* for every line skip 2 tabs to find the domain pos */
	BYTE *cursor = start;
	PRINTF("cursor = %p,\n", cursor);
	while(cursor < end){
		int loop = 2;
		while (loop > 0){
			//PRINTF("00003\n");
			if (*cursor == '\t'){
				loop--;
			}
			//PRINTF("00004\n");
			if (cursor == end){
				PRINTF("00004 break;\n");
				break;
			}
			//PRINTF("00005 -- loop %d\n", loop);
			cursor++;
		}
		//PRINTF("00000\n");
		/* find the 3rd field */
		if (!loop){
			//PRINTF("domain: %c%c%c%c%c\n", *cursor, *(cursor+1), *(cursor+2), *(cursor+3), *(cursor+4));
			/* parse domain */
			unsigned key = cal_crc(cursor);
			insert_key(pSeg->segno, key, cursor);
			
		}else{
			PRINTF("end of segment!\n");
			break;
		}
		//PRINTF("00001\n");
		/* skip line */
		while(cursor < end && *cursor != '\n'){
			cursor++;
		}
		//PRINTF("00002\n");
	}
	PRINTF("End thread No%d\n", pSeg->segno);
    return NULL;
}

